package com.pinyougou.manager.controller;

import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pinyougou.pojo.TbBrand;
import com.pinyougou.sellergoods.service.BrandService;

import entity.Result;
import entity.PageResult;

@RestController
@RequestMapping("/brand")
public class BrandController {
	@Reference
	private BrandService ibs;

	//查询所有商品
	@RequestMapping("/findAll")
	public List<TbBrand> findAll() {
		List<TbBrand> brandList = ibs.findAll();

		return brandList;
	}

	//分页查询
	@RequestMapping("findByPage")
	public PageResult findByPage(Integer pageNum, Integer pageSize) {
		PageResult findByPage = ibs.findByPage(pageNum, pageSize);
		return findByPage;
	}

	//添加商品
	@RequestMapping("addBrand")
	public Result addBrand(@RequestBody TbBrand brand) {
		try {
			ibs.addBrand(brand);
		} catch (Exception e) {
			return new Result(false, "添加失败");
			// TODO: handle exception
		}
		return new Result(true, "添加成功");

	}

	//通过id查找商品
	@RequestMapping("findById")
	public TbBrand findById(long id) {
		TbBrand brand = ibs.findById(id);
		return brand;
	}

	//更新商品信息
	@RequestMapping("updateBrand")
	public Result updateBrand(@RequestBody TbBrand brand) {
		try {
			ibs.update(brand);

		} catch (Exception e) {
			return new Result(false, "更改失败");
			// TODO: handle exception
		}
		return new Result(true, "更新成功");
	}

	@RequestMapping("delete")
	public Result deleteById(long[] id) {
		try {
			ibs.deleteById(id);
		} catch (Exception e) {
			return new Result(false, "删除失败");
			// TODO: handle exception
		}
		return new Result(true, "删除成功");
	}

	@RequestMapping("search")
	public PageResult search(@RequestBody TbBrand brand, Integer pageNum, Integer pageSize) {
		PageResult brandList = ibs.search(brand, pageNum, pageSize);
		return brandList;
	}

	@RequestMapping("selectOptionList")
	public List<Map> selectOptionList() {
		return ibs.selectOptionList();
	}
}
